package com.kky.pp.activity.me

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.custom.base.config.BaseConfig
import com.kky.pp.R
import com.kky.pp.popup.FeedbackPopup
import io.rong.imkit.event.EnumEventTag
import com.kky.pp.activity.member.MemberSubscriptionActivity
import com.kky.pp.activity.me.info.ProfileInfoActivity
import com.kky.pp.activity.settings.SettingActivity
import com.google.android.material.appbar.AppBarLayout
import com.gyf.immersionbar.ImmersionBar
import com.custom.base.mvp.MVPBaseFragment
import com.custom.base.base.SDBaseEvent
import com.kky.pp.databinding.FragmentFourBinding
import io.rong.imkit.SpName
import io.rong.imkit.utils.DensityUtil
import io.rong.imkit.utils.StatusBarUtil
import kotlin.math.abs

/**
 * 作者：
 * 时间：
 * 描述：我的
 */
class MeFragment : MVPBaseFragment<MeContract.View, MePresenter,FragmentFourBinding>(FragmentFourBinding::inflate), MeContract.View {

    override fun bindLayout(): Int {
        return R.layout.fragment_four
    }

    override fun initView(view: View) {
        fragmentViewBinding?.viewMeMian?.setPadding(0, StatusBarUtil.getStatusBarHeight(mActivity),0,0)
        fragmentViewBinding?.conMeFragHelpContainer?.setOnClickListener {
            FeedbackPopup(mActivity).showPopupWindow()
        }
        startActivity(ProfileInfoActivity::class.java,view=fragmentViewBinding?.imgViewMeFragUpdateHead)
        startActivity(ProfileInfoActivity::class.java,view=fragmentViewBinding?.conMeFragUpdateUserInfo)
        startActivity(SettingActivity::class.java,view=fragmentViewBinding?.conMeFragSettingContainer)
        startActivity(MemberSubscriptionActivity::class.java,view=fragmentViewBinding?.imgViewMeFragVipBg)

        fragmentViewBinding?.appBarMeFrag?.addOnOffsetChangedListener { _, verticalOffset ->
            val percentage = abs(verticalOffset).toDouble() / DensityUtil.dip2px(mActivity,50f)
            fragmentViewBinding?.imgViewMeFragHead?.alpha = 1f - percentage.toFloat()
            fragmentViewBinding?.imgViewMeFragUpdateHead?.alpha = 1f - percentage.toFloat()
            if (BaseConfig.getInstance.getBoolean(SpName.isMember,false)){
                fragmentViewBinding?.imgViewMeFragVipFlag?.alpha = 1f - percentage.toFloat()
                fragmentViewBinding?.imgViewMeFragHeadBorder?.alpha = 1f - percentage.toFloat()
            }
        }

        mPresenter?.getMemberData()

    }

    override fun onEventMainThread(event: SDBaseEvent) {
        when (EnumEventTag.valueOf(event.tagInt)) {
            EnumEventTag.MY_REFRESH -> {
                mPresenter?.getData()
            }
            else -> {}
        }
    }

    override fun onResume() {
        super.onResume()
        mPresenter?.getData()
        ImmersionBar.with(mActivity).statusBarDarkFont(false).init()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden){
//            ImmersionBar.with(this)
//                .statusBarColor("#f6f6f6")
//                .init()
            mPresenter?.getData()
        }
    }

    override val imgViewMeFragHead: ImageView
        get() = fragmentViewBinding?.imgViewMeFragHead!!
    override val txtMeFragNickName: TextView
        get() = fragmentViewBinding?.txtMeFragNickName!!

    override val conMeFragHelpContainer: ConstraintLayout
        get() = fragmentViewBinding?.conMeFragHelpContainer!!
    override val conMeFragSettingContainer: ConstraintLayout
        get() = fragmentViewBinding?.conMeFragSettingContainer!!
    override val conMeFragUpdateUserInfo: ConstraintLayout
        get() = fragmentViewBinding?.conMeFragUpdateUserInfo!!
    override val imgViewMeFragUpdateHead: ImageView
        get() = fragmentViewBinding?.imgViewMeFragUpdateHead!!

    override val imgViewMeFragVipBg: ImageView
        get() = fragmentViewBinding?.imgViewMeFragVipBg!!
    override val txtMeFragQuickChat: TextView
        get() = fragmentViewBinding?.txtMeFragQuickChat!!
    override val conMeFragQuickChat: ConstraintLayout
        get() = fragmentViewBinding?.conMeFragQuickChat!!
    override val txtMeFragQuickChatHor: TextView
        get() = fragmentViewBinding?.txtMeFragQuickChatHor!!
    override val conMeFragQuickChatHor: ConstraintLayout
        get() = fragmentViewBinding?.conMeFragQuickChatHor!!
    override val txtMeFragSecretPic: TextView
        get() = fragmentViewBinding?.txtMeFragSecretPic!!
    override val conMeFragMiddle: ConstraintLayout
        get() = fragmentViewBinding?.conMeFragMiddle!!
    override val conMeFragSecretPic: ConstraintLayout
        get() = fragmentViewBinding?.conMeFragSecretPic!!
    override val txtMeFragSecretVideo: TextView
        get() = fragmentViewBinding?.txtMeFragSecretVideo!!
    override val conMeFragSecretVideo: ConstraintLayout
        get() = fragmentViewBinding?.conMeFragSecretVideo!!
    override val txtMeFragVipTime: TextView
        get() = fragmentViewBinding?.txtMeFragVipTime!!
    override val txtMeFragVipGetPremium: TextView
        get() = fragmentViewBinding?.txtMeFragVipGetPremium!!
    override val txtMeFragVipDesc: TextView
        get() = fragmentViewBinding?.txtMeFragVipDesc!!
    override val txtMeFragVipTitle: TextView
        get() = fragmentViewBinding?.txtMeFragVipTitle!!
    override val conMeFragSecretAlbum: ConstraintLayout
        get() = fragmentViewBinding?.conMeFragSecretAlbum!!
    override val appBarMeFrag: AppBarLayout
        get() = fragmentViewBinding?.appBarMeFrag!!
    override val imgViewMeFragVipFlag: ImageView
        get() = fragmentViewBinding?.imgViewMeFragVipFlag!!
    override val imgViewMeFragHeadBorder: ImageView
        get() = fragmentViewBinding?.imgViewMeFragHeadBorder!!


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        mPresenter?.onActivityResult(requestCode, resultCode, data)
    }

}